// pages/films/player.js
Page({

  
  data: {

  },

  
  onLoad(options) {

  },

  
  onReady() {

  },

  
  onShow() {

  },

  onHide() {

  },


  onUnload() {

  },

  
  onPullDownRefresh() {

  },


  onReachBottom() {

  },

  
  onShareAppMessage() {

  }
})
Page({
	sources(e){ 
		console.log(e);
		let sourceIndex=e.target.dataset.index 
		let oldIndex=this.data.sourceIndex 
		if(oldIndex==sourceIndex)return 
		let change={sourceIndex} 
		if(oldIndex!=null)change[`sources[${oldIndex}].selected`]=false 
		change[`sources[${sourceIndex}].selected`]=true 
		this.setData(change) 
	},
	async onLoad(e){
		console.log(e)
		let index=parseInt(e.index) 
		let pages=getCurrentPages() 
		let pp=pages[pages.length-2] 
		console.log(pp.data) 
		let menu=pp.data.menus[pp.data.menuIndex] 
		let type=menu.types[menu.typeIndex] 
		let video=type.list[index] 
		console.log(video) 
		let url=`${pp.host}${video.url}` 
		let app=getApp() 
		let html=await app.request({url}) 
		
		let r, jj, regex=/video-info-content">(.+?)<\/div>/s 
		if(r=regex.exec(html)){
            jj='　　'+r[1].replace(/<\/?(br|p|div)[^>]*>|　+/g,'\n')
            .replace(/<[^>]*>/g,'')
            .replace(/&nbsp;/g,' ')
            .replace(/\s*\n\s*/g, '\n　　')
            .trim()
		}
		let sources=[] 
		regex=/value="([^"]+)/g 
		while(r=regex.exec(html)){
			sources.push({ 
				name: r[1],
			})
        }
        regex=/sort-item">(.+?)<\/div>/gs 
        let ind=-1
        while(r=regex.exec(html)){
            let r2, re=/title="([^"]+)" href="([^"]+)/g, list=[]
            while(r2=re.exec(r[1])){
                list.push({
                    name: r2[1], 
                    url: r2[2], 
                })
            }
            sources[++ind].list=list
        }
		this.setData({
			title: video.title,
			jj,
			sources,
		})
	},
})